import json
import time
import unittest

import allure
import pytest
from  selenium import webdriver
from selenium.webdriver.common.by import By

from Base.Base import Base
from Page.ShenHe_Page import ShenHe_Page
from Tools.Tools import log
from ddt import ddt,file_data

@pytest.fixture(scope="function")
def Case():
    with allure.step("启动浏览器"):
        driver=webdriver.Edge()
        driver.maximize_window()
    yield driver
    with allure.step("关闭浏览器"):
        time.sleep(3)
        driver.quit()
        print('测试结束')


@allure.feature('登录')
class TestLogin:
    @allure.title("管理员登录")
    @allure.story("用户登录功能")
    @allure.severity(allure.severity_level.NORMAL)
    @pytest.mark.parametrize("login_data",json.load(open("E:\GXPycharm\PythonObject\Help_PO_Ul\Data\login.json",encoding='utf-8')))
    def test_login(self,Case,login_data):
        username=login_data['username']
        password=login_data['password']
        eq=login_data['eq']
        with allure.step("登录-打开审核列表"):
            po = ShenHe_Page(Case)
            po.open_url('http://10.59.9.21/dist#/login')
            allure.attach(Case.get_screenshot_as_png(),name='打开登录页面',
                          attachment_type=allure.attachment_type.PNG)
            log.info('打开登录页面')

        with allure.step("输入账号密码"):
            po.username(username)
            po.password(password)
            allure.attach(Case.get_screenshot_as_png(),name='输入账号密码后截图',
                          attachment_type=allure.attachment_type.PNG)
            log.info('输入账号密码')
        with allure.step("点击登录"):
            po.login()
            allure.attach(Case.get_screenshot_as_png(),name='点击登录后截图',
                          attachment_type=allure.attachment_type.PNG)
            time.sleep(2)
            dy_login=Case.find_element(By.XPATH,'/html/body/div[2]').text
            assert dy_login==eq
            log.info(dy_login)

            #数据库断言：检查是否有登录记录
            base = Base(Case)  # 创建Base实例
            db = base.connect_db()  # 现在可以正确调用
            cursor=db.cursor()
            cursor.execute("SELECT * FROM  login_log WHERE username='admin' ORDER BY id DESC LIMIT 1")
            login_record=cursor.fetchone()
            assert  login_record is not None,'数据库中没有找到admin的登录记录'
            log.info('登录成功')